package com.china.produce.controller;

import com.alibaba.fastjson.JSON;
import com.china.produce.pojo.JackDate;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.SerializationFeature;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.text.SimpleDateFormat;
import java.util.Date;

/**
 * @ClassName
 * @Author niujie
 * @Version
 * @Description
 * @CreateTime 2023/2/28
 */
@RestController
@RequestMapping("/jackson")
public class JacksonController {

    @PostMapping("/test")
    public String test(@RequestBody JackDate dates) throws JsonProcessingException {

        ObjectMapper objectMapper = new ObjectMapper();
//        objectMapper.configure(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS, false);
//        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH");
//        objectMapper.setDateFormat(sdf);

        System.out.println(objectMapper.writeValueAsString(dates));

        System.out.println(dates.getDate());
        System.out.println(dates.getDate2());

        return null;
    }

    public static void main(String[] args) throws JsonProcessingException {
        ObjectMapper objectMapper = new ObjectMapper();
        objectMapper.configure(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS, false);
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH");
        objectMapper.setDateFormat(sdf);

        String jsonStr = "{\"date\":\"2021-02-02 12:40\",\"date2\":\"2021-02-02 13:50\"}";

        JackDate res = objectMapper.readValue(jsonStr, JackDate.class);
        System.out.println("date : " + res.getDate());
        System.out.println("date2 : " + res.getDate2());

        JackDate jackDate = JSON.parseObject(jsonStr, JackDate.class);
        System.out.println(jackDate.getDate());
        System.out.println(jackDate.getDate2());

        JackDate jt = new JackDate();
        jt.setDate(new Date());
        System.out.println(jt);

        System.out.println(objectMapper.writeValueAsString(jt));
    }
}
